package com.cxt.service.impl;//package com.baomidou.service.impl;
//
//import com.baomidou.entity.User;
//import com.baomidou.mapper.UserMapper;
//import com.baomidou.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxt.Mapper.UserMapper;
import com.cxt.context.BaseContext;
import com.cxt.entity.DriverInfo;
import com.cxt.entity.StudentInfo;
import com.cxt.entity.User;
import com.cxt.exception.BaseException;
import com.cxt.service.DriverInfoService;
import com.cxt.service.IAccountInfoService;
import com.cxt.service.IUserService;
import com.cxt.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-02-27
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private StudentInfoService  studentInfoService;
    @Autowired
    private DriverInfoService driverInfoService;
    @Autowired
    private IAccountInfoService accountInfoService;

    @Override
    public boolean register(User user) {
        //查询帐号是否传
       LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
       queryWrapper.eq(User::getPhone, user.getPhone());
        User one = this.getOne(queryWrapper);
        if(one !=null){
            throw  new BaseException("该手机号以注册");
        }
        //组装数据
        //密码加密
        user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
        user.setIsDriver(false);
        user.setIsBoundStudentInfo(false);
        this.save(user);
        //帐号重
        return true;
    }

    @Override
    public User login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getPhone, user.getPhone());
        User one = this.getOne(queryWrapper,false);
        if(one == null){
            return null;
        }
        if(!one.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
            return null;
        }
        return one;
    }

    @Override
    public boolean bindStudentInfo(StudentInfo studentInfo) {
        Long id = BaseContext.getCurrentId();
        studentInfo.setUserId(id);
        boolean save = studentInfoService.save(studentInfo);
        User byId = this.getById(id);
        byId.setIsBoundStudentInfo(true);
        this.updateById(byId);
        return true;
    }

    @Override
    public StudentInfo getStudentInfo() {
        LambdaQueryWrapper<StudentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentInfo::getUserId, BaseContext.getCurrentId());
        return studentInfoService.getOne(queryWrapper);
    }

    @Override
    public boolean updateStudentInfo(StudentInfo studentInfo) {
        //获取当前用户的studentInfo
        LambdaQueryWrapper <StudentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StudentInfo::getUserId, BaseContext.getCurrentId());
        StudentInfo one = studentInfoService.getOne(queryWrapper);
        studentInfo.setId(one.getId());
        return studentInfoService.updateById(studentInfo);
    }

    @Override
    public boolean addDriverInfo(DriverInfo driverInfo) {
        Long id = BaseContext.getCurrentId();
        driverInfo.setUserId(id);
        driverInfo.setStatus(0);
        driverInfoService.save(driverInfo);
        return true;
    }

    @Override
    public Integer getDriverInfoStatus() {
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<DriverInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverInfo::getUserId, id);
        DriverInfo one = driverInfoService.getOne(queryWrapper);
        if (one == null){
            return -1;
        }
        return one.getStatus();
    }

    @Override
    public boolean updateDriverInfo(DriverInfo driverInfo) {
        Long id = BaseContext.getCurrentId();
        LambdaQueryWrapper<DriverInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverInfo::getUserId, id);
        DriverInfo one = driverInfoService.getOne(queryWrapper);
        driverInfo.setId(one.getId());
        return driverInfoService.updateById(driverInfo);
    }
    @Override
    public DriverInfo getDriverInfo() {
        LambdaQueryWrapper<DriverInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverInfo::getUserId, BaseContext.getCurrentId());
        return driverInfoService.getOne(queryWrapper);
    }
    @Override
    public List<StudentInfo> getAllStudent() {
        List<StudentInfo> list = studentInfoService.list();
        return list;
    }
}
